<template>
    <div class="file-upload">
        <el-upload
            class="img-upload"
            accept="image"
            :show-file-list="false"
            action="/api/common/upload"
            :headers="headers"
            :on-success="handleUploadSuccess">
            <div v-if="uploadPicUrl === ''" class="box"></div>
            <div v-else class="pic-box">
            <img class="pic" :src="uploadPicUrl" alt="">
            <div class="pic-hover">
                <div class="delete" @click="deleteImg">删除图片</div>
                <div class="anew">重新上传</div>
            </div>
        </div>
        </el-upload>
        <div class="img-upload-tip">
          <span>图片大小不超过2M</span>
          <span>仅能上传 PNG JPEG JPG类型图片</span>
          <span>建议上传200*200或300*300尺寸的图片</span>
        </div>
    </div>
</template>

<script>
export default {
  name: 'FileUpload',
  data () {
    return {

    }
  },
  props: {
    uploadPicUrl: {
      type: String,
      default: ''
    },
    headers: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  methods: {
    handleUploadSuccess (res) {
      if (res.code === 1) {
        this.$emit('change', res.data)
      }
    },
    deleteImg (e) {
      e.stopPropagation()
      this.$emit('click')
    }
  }
}
</script>

<style lang="less" scoped>
.file-upload {
    display: flex;
    align-items: center;
}

label {
    width: 180px;
    text-align: end;
    margin-top: 5px;
}

.img-upload {

    ::v-deep .el-upload:focus {
        color: #333;
    }

    .box {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        width: 200px;
        height: 160px;
        border-radius: 5px;
        border: 1px dashed #d9d9d9;
        background-color: #fcfcfc;
        cursor: pointer;

        &:hover {
            border-color: #ffc100;
        }

        &::before {
            width: 20px;
            height: 20px;
            display: block;
            content: "";
            background: url('@/assets/icons/icon_upload@2x.png') no-repeat center;
            background-size: cover;
        }

        &::after {
            display: block;
            content: '上传图片';
        }
    }
}

.pic-box {
    position: relative;
    width: 200px;
    height: 160px;

    &:hover .pic-hover {
        opacity: 1;
    }

    .pic {
        width: 100%;
        height: 100%;
        border-radius: 5px;
        border: 1px solid #d9d9d9;
    }

    .pic-hover {
        top: 0;
        opacity: 0;
        transition: all .3s ease;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        position: absolute;
        background-color: rgba(0,0,0,.5);
        color: #fff;
        width: 100%;
        height: 100%;
        border-radius: 5px;
        gap: 20px;

        > div {
            border: 1px solid #fff;
            border-radius: 5px;
            cursor: pointer;
            padding: 5px 15px;
        }
    }
}

.img-upload-tip {
  display: flex;
  flex-direction: column;
  gap: 3px;
  color: #666;
  font-size: 12px;
  margin-left: 30px;
}
</style>
